package com.ysys.util;

import java.beans.PropertyDescriptor;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentFactory;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.xml.sax.InputSource;

/**
 * xml解析实体类
 * @author Wangc
 *
 */
public class Xmlutil {
	
	 /**
	  * 解析xml文件
	  * @param xml
	  * @return
	  */
	 public static Document parseDocument(String xml) {
		  InputSource in = new InputSource(new StringReader(xml));
		  in.setEncoding("UTF-8");
		  SAXReader reader = new SAXReader();
		  Document document = null;
		  try {
			  document = reader.read(in);
		  } catch (DocumentException e) {
			  e.printStackTrace();
		  }
		  return document;
	 }
	 
	 /**
	  * 讲xml转换成实体类导出
	  * @param xml
	  * @param T
	  * @return
	 * @throws Exception 
	  */
	 public static Object getObjectBean(String xml,Class<?> T,Object obj) throws Exception{
		 obj = T.newInstance();
		 Field[] fields = T.getClass().getDeclaredFields();
		 Document document = parseDocument(xml);
		 Element root = document.getRootElement();
		 if(fields != null && fields.length > 0){
			 for(Field f : fields) {
				 PropertyDescriptor pd = new PropertyDescriptor(f.getName(), T);
				 Method wM = pd.getWriteMethod();//获得写方法
				 wM.invoke(obj, root.element(fields[0].getName()));
			 }
			 return obj;
		 }
		 return null;
	 }
	    
    /** 
     * DOM4J生成XML 
     *  
     * @param list 
     * @return 
     */  
    public String dom4jToXml(Class<?> T,Object obj) throws Exception{  
        String strXml = null;  
        try {  
            if (null != T) {  
                DocumentFactory df = DocumentFactory.getInstance();
                org.dom4j.Document doc = df.createDocument("UTF-8");  
                // 创建根节点  
                org.dom4j.Element xmlElt = doc.addElement("xml");  
                Field[] fields = T.getFields();
                obj = T.newInstance();
                for (Field field : fields) {  
                org.dom4j.Element fieldElt = xmlElt.addElement(field.getName());
			 	PropertyDescriptor pd = new PropertyDescriptor(field.getName(), T);
			 	Method wM = pd.getReadMethod();//获得写方法
			 	fieldElt.addText(wM.invoke(obj).toString());
                }  
      
                // 有样式(缩进)的写出  
                OutputFormat opf = OutputFormat.createPrettyPrint();  
                opf.setEncoding("UTF-8");  
                opf.setTrimText(true);  
                  
                //生成XML文件  
                XMLWriter xmlOut = new XMLWriter(new FileOutputStream("dom4j.xml"), opf);  
                xmlOut.write(doc);  
                xmlOut.flush();  
                xmlOut.close();  
                  
                //获取XML字符串形式  
                StringWriter writerStr = new StringWriter();  
                XMLWriter xmlw = new XMLWriter(writerStr, opf);  
                xmlw.write(doc);  
                strXml = writerStr.getBuffer().toString();  
                  
                // 无样式的  
    //          strXml = doc.asXML();  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        System.out.println("DOM4J:" + strXml);  
        return strXml;  
    }  
}
